package com.ctbri.containerindepth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CountedString_Ex26 {
	private static List<String> created = new ArrayList<>();
	
	private String s;
	private int id = 0;
	private char c;
	
	public CountedString_Ex26(String str, char c) {
		s = str;
		this.c = c;
		created.add(s);
		for(String s2 : created) {
			if(s2.equals(s))
				id++;
		}
	}
	
	public String toString() {
		return "String: " + s + " id: " + id + " hashCode(): " + hashCode();
	}

	@Override
	public int hashCode() {
		final int prime = 37;
		int result = 17;
		result = prime * result + id;
		result = prime * result + ((s == null) ? 0 : s.hashCode());
		result = prime * result + (int)c;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		return obj instanceof CountedString_Ex26
				&& s.equals(((CountedString_Ex26)obj).s)
				&& c == ((CountedString_Ex26)obj).c
				&& id == ((CountedString_Ex26)obj).id;
	}
	
	public static void main(String[] args) {
		Map<CountedString_Ex26, Integer> map = new HashMap<>();
		CountedString_Ex26[] cs = new CountedString_Ex26[5];
		for(int i = 0; i < cs.length; i++) {
			cs[i] = new CountedString_Ex26("hi", (char)i);
			map.put(cs[i], i);
		}
		System.out.println(map);
		for(CountedString_Ex26 csstring : cs) {
			System.out.println("Looking up " + csstring);
			System.out.println(map.get(csstring));
		}
	}
}
